package com.sfl.api_backend.controller.files;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sfl.api_backend.constant.HttpCode;
import com.sfl.api_backend.entity.Datasets;
import com.sfl.api_backend.entity.Imgs;
import com.sfl.api_backend.entity.Labels;
import com.sfl.api_backend.entity.Labelsets;
import com.sfl.api_backend.mapper.DatasetsMapper;
import com.sfl.api_backend.mapper.LabelsetsMapper;
import com.sfl.api_backend.service.ILabelsService;
import com.sfl.api_backend.utils.Api.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标签表 前端控制器
 * </p>
 *
 * @author szc
 * @since 2023-01-27
 */
@RestController
@RequestMapping("/files/labels/label")
@Slf4j
public class LabelsController {

    @Resource
    private ILabelsService labelsService;

    @Resource
    private LabelsetsMapper labelsetsMapper;

    @PostMapping("/delect")
    @PreAuthorize("hasAuthority('User')")
    public RestResult delect(@RequestBody List<Integer> labelIds) {
        try {
            log.info("delect labels: {}", labelIds);

            List<Labels> labels = labelsService.listByIds(labelIds);
            for (Labels l : labels) {
                File file = new File(l.getLabelPath());
                if (file.exists()) {
                    file.delete();
                }
            }

            labelsService.removeBatchByIds(labelIds);
            return RestResult.success();
        }
        catch (Exception e) {
            log.error("delect labels: {} error", labelIds);
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('User')")
    public RestResult list(@RequestBody Labelsets vo) {
        try {
            log.info("list labels: {}", vo);
            Wrapper<Labels> wrapper = new LambdaQueryWrapper<>();
            ((LambdaQueryWrapper<Labels>) wrapper).eq(Labels::getLabelsetId, vo.getLabelsetId());
            List<Labels> labels = labelsService.list(wrapper);

            Labelsets labelsets = labelsetsMapper.selectById(vo.getLabelsetId());

            Map<String, Object> map = new HashMap<>();
            map.put("labelsets",labelsets);
            map.put("labels", labels);
            return RestResult.success(map);
        }
        catch (Exception e) {
            log.error("list labels: {} error", vo);
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }

    @PostMapping("/count")
    @PreAuthorize("hasAuthority('User')")
    public RestResult getObjCount(@RequestBody Labels vo) {
        try {
            log.info("list labels: {}", vo);
            return labelsService.getObjCount(vo);
        }
        catch (Exception e) {
            log.error("list labels: {} error", vo);
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }

    @PostMapping("/analysis")
    @PreAuthorize("hasAuthority('User')")
    public RestResult analysis(@RequestBody Labels vo){
        try{
            log.info("List labels: {}", vo.getLabelId());
            return labelsService.analysis(vo);
        }
        catch (Exception e){
            log.error("List labels: {} error", vo.getLabelId());
            return RestResult.error(HttpCode.NOT_IMPLEMENTED, e.getMessage());
        }
    }
}
